#ifndef UIWIN_CORE_CONTROL_H_
#define UIWIN_CORE_CONTROL_H_

#include "corrdinate.h"
#include <tchar.h>
#include <vector>
#include "gdiobj.h"

namespace uiwin
{
	enum ControlAlign
	{
		caNone,
		caLeft,
		caRight,
		caTop,
		caBottom,
		caClient
	};

	class Container;

	class DLL_API Control
	{
	public:
		Control(Container* parent);
		virtual ~Control();

		virtual void DoPaint(PaintDC& dc);

		virtual void Invalidate();

		Container* parent() const { return parent_; }
		LPVOID data() const { return data_; }

		virtual void Init() {}

		virtual void InvalidateRect(Rect& rc) {}
		void SetPos(int x, int y, int w, int h);

		void disable() { enable_ = false; }
		void setvisible(bool visible) { visible_ = visible; }

		LPCTSTR name() { return name_; }
		void setname(LPCTSTR lpszname);

		void setbkcolor(COLORREF color) { bkcolor_ = color; }

		LRESULT nchittest() { 
			return nchittest_; }

		virtual Control* FindControl(Point pt);
	protected:
		virtual void PaintBkColor(PaintDC& dc) {}
		virtual void PaintBkImage(PaintDC& dc) {}
		virtual void PaintStatusImage(PaintDC& dc) {}
		virtual void PaintText(PaintDC& dc) {}
		virtual void PaintBorder(PaintDC& dc) {}

		// position
		ControlAlign align_;	// align
		Rect  position_;		// position offset by parent
		Rect  rcitem_;			// position offset by window
		int   radiosize_;

		// border and background
		COLORREF bkcolor_;
		COLORREF bordercolor_;
		int bordersize_;

		// visible and enable
		bool visible_;
		bool enable_;
		bool focused_;

		LPTSTR name_;
		LPTSTR text_;
		LPTSTR tooltip_;

		LPVOID data_;

		Container* parent_;
		bool indesign_;

		LRESULT nchittest_;
	};

	//class Scrollbar;

	class DLL_API Controls
	{
	public:
		Controls(int preallocatesize = 0);
		Controls(const Controls& src);
		virtual ~Controls();

		void empty();
		bool isempty() const { return count_ == 0; }
		int size() const { return count_; }
		void resize(int size);

		int find(Control* obj) const;
		Control* get(int index)const;
		Control* operator[](int index) const;		
		
		bool add(Control* obj);
		bool setat(int index, Control* obj);
		bool insertat(int index, Control* obj);
		bool remove(int index);


	private:
		bool grow();
	protected:
		Control** obj_pointer_;
		int count_;
		int allocated_;
	};

	class DLL_API Container : public Control//, public ContainerInterface
	{
	public:
		Container(Container* parent);
		
		virtual void DoPaint(PaintDC& dc);

		// children
		virtual Control* FindControl(Point pt);

		virtual bool AddControl(Control* ctrl);
		virtual void Remove(Control* ctrl);
		virtual bool RemoveAt(int index);
		virtual void Clear();


	protected:
		virtual void PaintChildren(PaintDC& dc);
		virtual void PaintBkColor(PaintDC& dc) {Brush b; b.CreateSolidBrush(bkcolor_); dc.FillRect(&rcitem_, b);}

		Controls children_;
	};
}


#endif	// UIWIN_CORE_CONTROL_H_